const mysqlHelper = require('./../db/mysql-helper.js')
const mysqlorm = require('./../db/mysqlorm')
const mysql = require("mysql")

const Sequelize = require('sequelize');
const Op = Sequelize.Op;

/* eslint no-return-await:0 */
class StationModel {

  // async getStaName(args) {
  //   let sql = "select STA_NAME from SYS_Station where STA_NAME is not null GROUP BY STA_NAME"
  //   let result = await mysqlHelper.query(sql)
  //   return result
  // }

  async getStaName(args) {
    // console.log(1111111111, args)

    let ormParams = {
      where: {
        STA_NAME: {
          [Op.ne]: null,
        },

      },
    };
    if (args.OUName.length != 0) {
      ormParams.where["OUName"] = {}
      ormParams.where.OUName = {
        [Op.in]: [args.OUName]
      }
    }

    // if (args.staName.length != 0) {
    //   ormParams.where["STA_NAME"] = args.staName
    // }
    // if (args.ouName.length != 0) {
    //   ormParams.where["OUName"] = args.ouName
    // }

    console.log(ormParams)
    let asyncSql = mysqlorm.SYS_STATION.findAll({
      where: ormParams.where,
      group: 'STA_NAME',// 不输出这几列，因为没有
      attributes: ['STA_NAME']
    });


    let result = await mysqlorm.Query(asyncSql)
    // console.log("S", result)

    return result
  }


  // async getOuName(args) {
  //   let sql = "select OUName from SYS_Station where OUName is not null GROUP BY OUName"
  //   let result = await mysqlHelper.query(sql)
  //   return result
  // }

  async getOuName(args) {
    console.log(args)
    let ormParams = {
      where: {
        OUName: {
          [Op.ne]: null
        }
      },
    };
    // if (args.staName.length != 0) {
    //   ormParams.where["STA_NAME"] = args.staName
    // }
    // if (args.ouName.length != 0) {
    //   ormParams.where["OUName"] = args.ouName
    // }


    console.log(ormParams)
    let asyncSql = mysqlorm.SYS_STATION.findAll({
      where: ormParams.where,
      group: 'OUName',// 不输出这几列，因为没有
      attributes: ['OUName']
    });


    let result = await mysqlorm.Query(asyncSql)
    // console.log("S", result)

    return result
  }

  async delStaData(args) {

    let v = args.value

    let sql = "update SYS_Station as t set t.IsImgDelete=? where t.STA_ID=?";
    let param = [1, v.STA_ID]

    let result = await mysqlHelper.query(sql, param)

    return result
  }


  async updateStaData(args) {

    let v = args.value

    let sql = "update SYS_Station as t set t.STA_NAME = ?,t.STA_LINENAME=?,t.OUCode=?,\n" +
      "t.OUName = ?,t.STA_ORDER=? where t.STA_ID=?"
    let param = [v.STA_NAME, v.STA_LINENAME, v.OUCode, v.OUName, parseInt(v.STA_ORDER), v.STA_ID]

    let result = await mysqlHelper.query(sql, param)

    return result
  }

  async creatStaData(args) {

    let v = args.value
    console.log(args.value)
    let sql = "select OUCode from SYS_Station limit 1"
    let oucode = await mysqlHelper.query(sql) // 先提取 OUName对应的OUCode

  console.log(oucode[0].OUCode)
    sql = "INSERT INTO SYS_Station (STA_ID,STA_ORDER, STA_NAME, STA_LINENAME, STA_ATTRIB, OUCode, OUName,IsImgDelete) " +
      "VALUES (?, ?, ?, ?, ?, ?,?,?)";
    let param = [v.STA_ID, v.STA_ORDER, v.STA_NAME, v.STA_LINENAME, v.STA_ATTRIB, oucode[0].OUCode, v.OUName, v.IsImgDelete]

    let result = await mysqlHelper.query(sql, param)

    return result
  }

  async getDataByFilter(args) {

    // let sql = "select * from SYS_Station  where";
    // let offset = (args.current - 1) * args.pageSize;
    // let whereParam = []
    //
    //
    // if (args.staName.length == 0) {
    //   sql = sql + " 1=1"
    // } else {
    //   sql = "select * from `SYS_Station` as s where  `STA_NAME` in (?)"
    //   // sql = "select STA_NAME from SYS_Station where STA_NAME =(?"
    //
    //   whereParam.push(args.staName.toString())
    // }
    //
    // if (args.ouName.length == 0) {
    //   sql = sql + " and 1=1"
    // } else {
    //   sql = sql + " and `OUName` in (?)";
    //   whereParam.push(args.ouName.toString())
    // }
    // sql = sql + " limit ?,?";
    //
    // whereParam = whereParam.concat([offset, args.pageSize])
    // let result = await mysqlHelper.query(mysql.format(sql,whereParam))


    // todo orm
    console.log(args)
    let ormParams = {
      where: {
        //     [Op.or]:{
        // {"IsImgDelete":[Op.eq]: null,}
        //   {     "IsImgeDelte":[Op.notIn]:[1]}
        //     }
        [Op.or]: [
          {IsImgDelete: {[Op.eq]: null}},
          {IsImgDelete: {[Op.notIn]: [1]}}
        ]
      },
      offset: 0,
      limit: 10
    };
    if (args.staName.length != 0) {
      ormParams.where["STA_NAME"] = args.staName
    }
    if (args.ouName.length != 0) {
      ormParams.where["OUName"] = args.ouName
    }

    ormParams.offset = (args.current - 1) * args.pageSize
    ormParams.limit = args.pageSize
    console.log(ormParams)
    let asyncSql = mysqlorm.SYS_STATION.findAndCountAll({
      where: ormParams.where,
      offset: ormParams.offset,
      limit: ormParams.limit,
      attributes: {exclude: ['id', 'createdAt', 'updatedAt']} // 不输出这几列，因为没有
    });


    let result = await mysqlorm.Query(asyncSql)
    // console.log("S", result)

    return result
  }

  // async getListByFilter(args) {
  //   let sql = "SELECT Id, UserName, UserPass FROM userinfo WHERE UserName like ? limit ? ,? "
  //   let whereParam
  //   if (args.where.username != null) {
  //     whereParam = "%" + args.where.username + "%"
  //   }
  //   let params = [whereParam, args.offset, args.limit]
  //   let result = await mysqlHelper.query(sql, params)
  //   return result
  // }

//   async getCountByFilter(args) {
//     let sql = "select count(1) from SYS_Station where"
//     let whereParam = []
//
//     if (args.ouName.length == 0) {
//       sql = sql + " 1=1"
//     } else {
//       sql = "select count(1) from SYS_Station where OUName in (?)"
//       whereParam.push(args.ouName.toString())
//     }
//     if (args.staName.length == 0) {
//       sql = sql + " and 1=1"
//     } else {
//       sql = sql + " and sta_name in (?) limit 1";
//       whereParam.push(args.staName.toString())
//     }
//
//
//     let result = await mysqlHelper.query(sql, whereParam)
//     return result
//   }
//
}

module.exports = new StationModel();
